/**
 * @date 2006-1-6 19:23:09
 *
 */
package org.nanhill.commons.html;

import static org.nanhill.commons.html.XhtmlConstants.BORDER;
import static org.nanhill.commons.html.XhtmlConstants.CELLPADDING;
import static org.nanhill.commons.html.XhtmlConstants.CELLSPACING;
import static org.nanhill.commons.html.XhtmlConstants.COLSPAN;
import static org.nanhill.commons.html.XhtmlConstants.ROWSPAN;
import static org.nanhill.commons.html.XhtmlConstants.TABLE;
import static org.nanhill.commons.html.XhtmlConstants.TABLE_START_OPEN;
import static org.nanhill.commons.html.XhtmlConstants.TD;
import static org.nanhill.commons.html.XhtmlConstants.TD_START_OPEN;
import static org.nanhill.commons.html.XhtmlConstants.TR;
import static org.nanhill.commons.html.XhtmlConstants.TR_START;
import static org.nanhill.commons.lang.StringConstants.BLANK_CHAR;
import static org.nanhill.commons.lang.StringConstants.EQUAL;
import static org.nanhill.commons.lang.StringConstants.GT;
import static org.nanhill.commons.lang.StringConstants.QUOT;
import static org.nanhill.commons.xml.XmlConstants.LTS;

import java.io.Reader;

import org.nanhill.commons.xml.xpp.XmlPullParser;
import org.nanhill.commons.xml.xpp.kxml.io.KXmlParser;

/**
 * @author alin
 * 
 */
public class XppXhtmlTableTidy {
	private XmlPullParser xpp;

	private StringBuilder builder;

	private void buildAttr(String attrName, String attrValue) {
		builder.append(BLANK_CHAR);
		builder.append(attrName);
		builder.append(EQUAL);
		builder.append(QUOT);
		builder.append(attrValue);
		builder.append(QUOT);
	}

	private void buildEndTag(String tagName) {
		builder.append(LTS);
		builder.append(tagName);
		builder.append(GT);
	}

	private void buildTable() {
		builder.append(TABLE_START_OPEN);
		String attrValue = xpp.getAttributeValue(null, BORDER);
		if (null != attrValue) {
			buildAttr(BORDER, attrValue);
		}
		attrValue = xpp.getAttributeValue(null, CELLPADDING);
		if (null != attrValue) {
			buildAttr(CELLPADDING, attrValue);
		}
		attrValue = xpp.getAttributeValue(null, CELLSPACING);
		if (null != attrValue) {
			buildAttr(CELLSPACING, attrValue);
		}
		builder.append(GT);
	}

	private void buildTr() {
		builder.append(TR_START);
	}

	private void buildTd() {
		builder.append(TD_START_OPEN);
		String attrValue = xpp.getAttributeValue(null, ROWSPAN);
		if (null != attrValue) {
			buildAttr(ROWSPAN, attrValue);
		}
		attrValue = xpp.getAttributeValue(null, COLSPAN);
		if (null != attrValue) {
			buildAttr(COLSPAN, attrValue);
		}
		builder.append(GT);
	}

	public String tidy(Reader reader) {
		builder = new StringBuilder();
		xpp = new KXmlParser();
		xpp.setInput(reader);
		int eventType = xpp.getEventType();
		do {
			if (eventType == XmlPullParser.START_TAG) {
				String tagName = xpp.getName();
				if (tagName.equals(TABLE)) {
					buildTable();
				} else if (tagName.equals(TR)) {
					buildTr();
				} else if (tagName.equals(TD)) {
					buildTd();
				}
			} else if (eventType == XmlPullParser.END_TAG) {
				String tagName = xpp.getName();
				buildEndTag(tagName);
			} else if (eventType == XmlPullParser.TEXT) {
				builder.append(xpp.getText());
			}
			eventType = xpp.next();
		} while (eventType != XmlPullParser.END_DOCUMENT);

		return builder.toString();
	}
}
